package com.gec.zhs.controller;

import com.gec.zhs.annotation.ZhsLogin;
import com.gec.zhs.entity.constants.Constants;
import com.gec.zhs.entity.pojo.VideoDanmu;
import com.gec.zhs.entity.pojo.VideoInfo;
import com.gec.zhs.entity.query.VideoDanmuQuery;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.service.VideoDanmuService;
import com.gec.zhs.service.VideoInfoService;
import com.gec.zhs.service.impl.VideoInfoServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-02-14
 * @description 视频弹幕
 */
@RestController
@RequestMapping("/danmu")
@Slf4j
public class VideoDanmuController extends ABaseController {

    @Autowired
    private VideoDanmuService videoDanmuService;

    @Autowired
    private VideoInfoService videoInfoService;

    @ApiOperation("发送视频弹幕")
    @RequestMapping("/postDanmu")
    @ZhsLogin(checkLogin = true)
    public ResponseVO postDanmu(String videoId, String fileId,
                                String text, Integer mode,
                                String color, Integer time) {
        VideoDanmu videoDanmu = new VideoDanmu();
        videoDanmu.setVideoId(videoId);
        videoDanmu.setFileId(fileId);
        videoDanmu.setText(text);
        videoDanmu.setMode(mode);
        videoDanmu.setColor(color);
        videoDanmu.setTime(time);
        videoDanmu.setUserId(getTokenUserInfoDto().getUserId());
        videoDanmu.setPostTime(new Date());

        videoDanmuService.saveVideoDanmu(videoDanmu);
        return getSuccessResponseVO(null);
    }

    @ApiOperation("加载弹幕")
    @RequestMapping("/loadDanmu")
    public ResponseVO loadDanmu(String fileId, String videoId) {
        VideoInfo videoInfo =  videoInfoService.getVideoInfoByVideoId(videoId);
        if(videoInfo.getInteraction() != null&& !videoInfo.getInteraction().contains(Constants.ZERO.toString())){
            return getSuccessResponseVO(new ArrayList<>());
        }

        VideoDanmuQuery videoDanmuQuery = new VideoDanmuQuery();
        videoDanmuQuery.setFileId(fileId);
        videoDanmuQuery.setOrderBy("danmu_id asc");
        return getSuccessResponseVO(videoDanmuService.findListByParam(videoDanmuQuery));
    }

}
